package com.mes.loco.aps.server.controller.rsm;

import com.iplant.aps.controller.BaseController;
import com.iplant.aps.service.MyHelperService;
import com.iplant.aps.service.po.ServiceResult;
import com.iplant.aps.service.po.bms.BMSEmployee;
import com.iplant.aps.service.po.rsm.RSMTurnOrderDesc;
import com.iplant.aps.service.utils.CloneTool;
import com.iplant.aps.service.utils.StringUtils;
import com.iplant.aps.utils.RetCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author PengYouWang
 * 2023-3-21 15:17:13
 */
@RestController
@RequestMapping("/api/RSMTurnOrderDesc")
public class RSMTurnOrderDescController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(RSMTurnOrderDescController.class);

    final
    MyHelperService wRSMService;

    public RSMTurnOrderDescController(MyHelperService wRSMService) {
        this.wRSMService = wRSMService;
    }

    /**
     * 条件查询
     */
    @GetMapping("/All")
    public Object All(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            int wOrderID = StringUtils.parseInt(request.getParameter("OrderID"));
            int wPartID = StringUtils.parseInt(request.getParameter("PartID"));
            int wPartPointID = StringUtils.parseInt(request.getParameter("PartPointID"));
            int wIsMaterialShortage = StringUtils.parseInt(request.getParameter("IsMaterialShortage"));

            ServiceResult<List<RSMTurnOrderDesc>> wServiceResult = wRSMService.RSM_QueryTurnOrderDescList(wLoginUser,
                    wOrderID, wPartID, wPartPointID, wIsMaterialShortage);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 新增或更新
     */
    @PostMapping("/Update")
    public Object Update(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            if (!wParam.containsKey("data"))
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);

            RSMTurnOrderDesc wRSMTurnOrderDesc = CloneTool.Clone(wParam.get("data"), RSMTurnOrderDesc.class);
            if (wRSMTurnOrderDesc == null || wRSMTurnOrderDesc.OrderID <= 0 || wRSMTurnOrderDesc.PartID <= 0 || wRSMTurnOrderDesc.PartPointID <= 0)
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);

            ServiceResult<Integer> wServiceResult = wRSMService.RSM_UpdateTurnOrderDesc(wLoginUser, wRSMTurnOrderDesc);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }
}
